﻿toggle = function (fsid)
{
//    var div = $id("div_" + fsid);
//    var btn = $id("btn_" + fsid);
//    var btnEdit = $id("btnEdit_" + fsid);
//    var load = $id("load_" + fsid);
//        
//    var display = "";
//    
//    if( div.style.display != "none" )
//    {
//        display = "none";
//    }
//    
//    div.style.display = display;
//    btn.style.display = display;
//    btnEdit.style.display = display;
//    load.style.display = display;
}

CD_New = function(iCustID)
{
    //alert("save" + iCustID);
    var load = $id("load");
    var btn = $id("btn");
    var btnCancel = $id("btnCancel");
    if(confirm("save changes?"))
    {
        // di sini kirim data perubahannya
        var input = getData(iCustID);
        //alert(input);
        if(input)
        {
            btn.style.display = "none";
            btnCancel.style.display = "none";
            load.innerHTML = "<img src='" + LOADING.src + "' alt='' />"; 
            cbkCDNew_c.PerformCallback("SAVE" + DELIMITER + input);
        }
        else
        {
            btn.style.display = "inline";
            btnCancel.style.display = "inline";
            load.innerHTML = "";
        }
    }
}

getData = function(iCustID)
{
    var inputs = $nm("txt");
    var output = "";
    //var iEmptyValueCount = 0;
    var iNotUploadedCount = 0;
    
    output += DELIMITER + "CD.CUSTOMERID" + NEWLINE + iCustID;
    output += DELIMITER
    for( var i=0; i < inputs.length; i++ )
    {
        if( i > 0 )
        {
            output += DELIMITER;
        }
        
        var item = inputs[i];
        var lov = $id("lov_" + item.id);
        
        if( lov == null )
        {
            //if(item.value == "")
            //{
            //    iEmptyValueCount++;
            //}
            if((item.id == "CD.CONFIGFILEID" || item.id == "CD.EVALUASIFILEID" || item.id == "CD.DATEKFILEID") && item.value == "")
            {
                iNotUploadedCount++;
            }
            else
            {
                output += item.id + NEWLINE + item.value;
            }
        }
        else
        {
            var lovh = $id("lovh_" + item.id);
            output += lov.value + NEWLINE + lovh.value + NEWLINE + item.value;
            
            var uplbtn = $id("uplbtn_" + item.id);
            if( uplbtn != null && item.value != "" )
            {
                item.style.backgroundColor = "#FFAAAA";
                iNotUploadedCount++;
            }       
        }
    }
    
    var sxError = "";
    
    if( iNotUploadedCount == 3 )
    {
        sxError += "File belum di upload!";
    }
    
    if( sxError != "" )
    {
        alert(sxError);
        return false;
    }
    
    return output;
}

cbkCDNew_c_CallbackComplete = function(e) {
    var selector = e.result.split(DELIMITER)[0];
    
    if (selector == "LOAD")
    {
        //alert(e.result);
        var div = $id("div_loaded");
        var div_CDNew = $id("div_CDNew");
        div_CDNew.innerHTML = "";
        div.innerHTML = e.result.split(DELIMITER)[1];
    }
    else if (selector == "SAVE")
    {
        //var div = $id("div_loaded");
        //var div_CDNew = $id("div_CDNew");
        //div_CDNew.innerHTML = "";
        //div.innerHTML = e.result.split(DELIMITER)[1];
        window.location = "CDList.aspx";
    }
    else if (selector == "CANCEL")
    {
        var div = $id("div_loaded");
        var div_CDNew = $id("div_CDNew");
        div_CDNew.innerHTML = e.result.split(DELIMITER)[1];
        div.innerHTML = ""; 
    }
    else if (selector == "EDIT")
    {
        //var div = $id("div_loaded");
        //var div_CDNew = $id("div_CDNew");
        //div_CDNew.innerHTML = "";
        //div.innerHTML = e.result.split(DELIMITER)[1];
        window.location = "CDList.aspx";
    }
}

SelectCompany = function()
{
    var lovhCompany = $id("lovh_CD.CUSTOMERID");
    //alert(lovhCompany.value);
    cbkCDNew_c.PerformCallback("LOAD" + DELIMITER + lovhCompany.value);
}
SelectNetworkInfo = function()
{
    var lovNetwork = $id("lov_CD.NETWORK");
    var txtInfo = $id("CD.INFO");
    
    txtInfo.value += lovNetwork.value;
}
cancel = function()
{
    var load = $id("load");
    var btn = $id("btn");
    var btnCancel = $id("btnCancel");
    if (load != null)
    {
        btn.style.display = "none";
        btnCancel.style.display = "none";
        load.innerHTML = "<img src='" + LOADING.src + "' alt='' />";
    }
    cbkCDNew_c.PerformCallback("CANCEL");
}
edit = function(iCustID,iCDID)
{
    //alert("edit" + iCustID);
    var load = $id("load");
    var btn = $id("btn");
    var btnCancel = $id("btnCancel");
    if(confirm("edit data?"))
    {
        // di sini kirim data perubahannya
        var input = getDataEdit(iCustID,iCDID);
        //alert(input);
        if(input)
        {
            btn.style.display = "none";
            btnCancel.style.display = "none";
            load.innerHTML = "<img src='" + LOADING.src + "' alt='' />"; 
            cbkCDNew_c.PerformCallback("EDIT" + DELIMITER + input);
        }
        else
        {
            btn.style.display = "block";
            btnCancel.style.display = "block";
            load.innerHTML = "";
        }
    }
}
getDataEdit = function(iCustID, iCDID)
{
    var inputs = $nm("txt");
    var output = "";
    //var iEmptyValueCount = 0;
    var iNotUploadedCount = 0;
    
    output += DELIMITER + "CD.CUSTOMERID" + NEWLINE + iCustID;
    output += DELIMITER + "CD.ID" + NEWLINE + iCDID;
    output += DELIMITER
    for( var i=0; i < inputs.length; i++ )
    {
        if( i > 0 )
        {
            output += DELIMITER;
        }
        
        var item = inputs[i];
        var lov = $id("lov_" + item.id);
        
        if( lov == null )
        {
            //if(item.value == "")
            //{
            //    iEmptyValueCount++;
            //}
            if((item.id == "CD.CONFIGFILEID" || item.id == "CD.EVALUASIFILEID" || item.id == "CD.DATEKFILEID") && item.value == "")
            {
                iNotUploadedCount++;
            }
            else
            {
                output += item.id + NEWLINE + item.value;
            }
        }
        else
        {
            var lovh = $id("lovh_" + item.id);
            output += lov.value + NEWLINE + lovh.value + NEWLINE + item.value;
            
            var uplbtn = $id("uplbtn_" + item.id);
            if( uplbtn != null && item.value != "" )
            {
                item.style.backgroundColor = "#FFAAAA";
                iNotUploadedCount++;
            }       
        }
    }
    
    var sxError = "";
    
    if( iNotUploadedCount == 3 )
    {
        sxError += "File belum di upload!";
    }
    
    if( sxError != "" )
    {
        alert(sxError);
        return false;
    }
    
    return output;
}